package com.wq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wq.BusinessException;
import com.wq.dto.DepartmentDTO;
import com.wq.entity.Department;
import com.wq.enums.RespEnum;
import com.wq.mapper.DepartmentMapper;
import com.wq.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wq
 * @since 2025-11-09
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Override
    public Page<Department> getDepartmentList(Integer pageNum, Integer pageSize, String keyword) {
        Page<Department> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
        
        if (keyword != null && !keyword.trim().isEmpty()) {
            wrapper.like(Department::getDeptName, keyword)
                    .or()
                    .like(Department::getDeptCode, keyword);
        }
        
        wrapper.orderByAsc(Department::getSort)
                .orderByDesc(Department::getCreateTime);
        
        return this.page(page, wrapper);
    }

    @Override
    public List<Department> getAllDepartments() {
        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Department::getStatus, true)
                .orderByAsc(Department::getSort);
        return this.list(wrapper);
    }

    @Override
    public Department addDepartment(DepartmentDTO departmentDTO) {
        // 检查部门编码是否已存在
        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Department::getDeptCode, departmentDTO.getDeptCode());
        Department existDept = this.getOne(wrapper);
        
        if (existDept != null) {
            throw new BusinessException(RespEnum.INVALID_PARAMETERS);
        }
        
        Department department = new Department();
        BeanUtils.copyProperties(departmentDTO, department);
        
        this.save(department);
        return department;
    }

    @Override
    public Boolean updateDepartment(Integer id, DepartmentDTO departmentDTO) {
        Department department = this.getById(id);
        if (department == null) {
            throw new BusinessException(RespEnum.INVALID_PARAMETERS);
        }
        
        // 检查部门编码是否被其他部门使用
        if (departmentDTO.getDeptCode() != null && !departmentDTO.getDeptCode().equals(department.getDeptCode())) {
            LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Department::getDeptCode, departmentDTO.getDeptCode())
                    .ne(Department::getId, id);
            Department existDept = this.getOne(wrapper);
            
            if (existDept != null) {
                throw new BusinessException(RespEnum.INVALID_PARAMETERS);
            }
        }
        
        BeanUtils.copyProperties(departmentDTO, department);
        department.setId(id);
        return this.updateById(department);
    }

    @Override
    public Boolean deleteDepartment(Integer id) {
        Department department = this.getById(id);
        if (department == null) {
            throw new BusinessException(RespEnum.INVALID_PARAMETERS);
        }
        
        // TODO: 检查部门下是否有员工，如有则不允许删除
        
        return this.removeById(id);
    }
}
